import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { LocalStorage } from '../../utils/localstorage';
import { HttpService } from "../../http/http.service";
import { NotifyService } from "../../utils/notify.service";
import { NzModalRef } from 'ng-zorro-antd/modal';


@Component({
  selector: 'updatepwd',
  templateUrl: './updatepwd.component.html',
  styleUrls: [ './updatepwd.component.styl' ]
})
export class UpdatepwdComponent implements OnInit {

  form: FormGroup;

  @Input() parent;

  oldPwdVisible = false;
  newPwdVisible = false;
  chkpwdVisible = false;
  isLoading = false;

  constructor(
    private fb: FormBuilder,
    private http: HttpService,
    private notify: NotifyService
    ) {
  }

  confirmValidator = (control: FormControl): { [s: string]: boolean } => {

    if (!control.value) {
      return { required: true, error: true };
    } else if (control.value !== this.form.controls.newpwd.value) {
      return { confirm: true, error: true };
    }
    return {};
  };

  newPwdValidator = (control: FormControl): { [s: string]: boolean } => {
    if (this.form && this.form.controls && this.form.controls.oldpwd) {
      if (control.value === this.form.controls.oldpwd.value) {
        return { samepwd: true, error: true };
      }
    }
    return {};
  };

  ngOnInit() {
    let info: any = LocalStorage.getInfoObject();

    this.form = this.fb.group({
      fullName: [ info.userName ],
      oldpwd: [ null, [ Validators.required, Validators.maxLength(10) ] ],
      newpwd: [ null, [ Validators.required, Validators.minLength(6), Validators.maxLength(10), this.newPwdValidator ] ],
      chkpwd: [ null, [ this.confirmValidator ] ],
    });
    this.form.get("fullName").setValue(info.userName);
  }

  get oldpwd() {
    return this.form.get('oldpwd');
  }

  get newpwd() {
    return this.form.get('newpwd');
  }

  get chkpwd() {
    return this.form.get('chkpwd');
  }

  onClose():void{
    this.parent.modalPwd.close();
  }

  onSave(): void {
    for (const i in this.form.controls) {
      this.form.controls[i].markAsDirty();
      this.form.controls[i].updateValueAndValidity();
    }
    if (!this.form.valid) return;
    let saveDate: any = {};

    let info: any = LocalStorage.getInfoObject();

    saveDate.userid = info.userId;
    saveDate.oldpwd = this.form.get("oldpwd").value;
    saveDate.newpwd = this.form.get("newpwd").value;
    saveDate.chkpwd = this.form.get("chkpwd").value;

    this.notify.confirmMsg("001008").subscribe(res => {
      if (res === 0) {
        this.isLoading = true;
        this.http.post("bas/user/updpwd", null, saveDate).subscribe(res => {
          this.isLoading = false;
          if (res.err == '0') {
            this.form.get("fullName").setValue(info.userName);
            this.notify.successMsg("001009").subscribe(res=>{
              this.onClose();
            });

          } else {
            this.notify.error(res.errMsg).subscribe();
          }
        });

      }
    });


  }

}
